package com.wb3.organizer.converter;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Map;

import org.apache.struts2.util.StrutsTypeConverter;

public class TimeTypeConverter extends StrutsTypeConverter {
	
	private static SimpleDateFormat sf = new SimpleDateFormat("hh:mm a");
	
	@Override
	public Object convertFromString(Map context, String[] value, Class toType) {
		String[] times = value[0].split(":");
		Calendar c = Calendar.getInstance();
		c.set(Calendar.HOUR_OF_DAY, Integer.parseInt(times[0]));
		c.set(Calendar.MINUTE, Integer.parseInt(times[1]));
		
		if(times[2].equals("PM")){
			c.set(Calendar.HOUR_OF_DAY, Integer.parseInt(times[0]) + 12);
		}
		return c.getTime();
	}

	@Override
	public String convertToString(Map context, Object value) {
		return sf.format(value);
	}

}
